﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using GalaSoft.MvvmLight.Command;
using MishraReader.AppServices;
using MishraReader.Entities;
using MishraReader.ViewModels.Base;

namespace MishraReader.ViewModels.Reader
{
    public interface IShareViewModel : IDialogViewModel
    {
        /// <summary>
        ///   Gets the item.
        /// </summary>
        Item Item { get; set; }
    }

    public class ShareViewModel : ShareViewModelBase, IShareViewModel
    {
        private readonly IWpfApplication _application;
        private readonly IOptionsManager _optionsManager;
        private readonly IShareServiceManager _serviceManager;
        private String _message;

        public ShareViewModel(Item item,
                              IOptionsManager optionsManager,
                              ILightWeakEventManager eventManager,
                              IShareServiceManager serviceManager,
                              Func<IShareServicePackage, bool, ShareServiceViewModel> shareVmFactory,
                              IWpfApplication application)
            : base(item, ServiceType.Share, optionsManager, eventManager, shareVmFactory)
        {
            _optionsManager = optionsManager;
            _serviceManager = serviceManager;
            _application = application;
            ShareCommand = new RelayCommand(ExecuteShareCommand, CanExecuteShareCommand);
        }

        /// <summary>
        ///   Gets or sets the message.
        /// </summary>
        /// <value> The message. </value>
        public String Message
        {
            get { return _message; }
            set
            {
                if (_message != value)
                {
                    _message = value;
                    _application.RefreshCommands();
                    RaisePropertyChanged();
                }
            }
        }

        /// <summary>
        ///   Gets the share command.
        /// </summary>
        public ICommand ShareCommand { get; private set; }


        protected override void OnActivatedServiceChanged()
        {
            _application.RefreshCommands();
            base.OnActivatedServiceChanged();
        }

        private bool CanExecuteShareCommand()
        {
            return String.IsNullOrEmpty(Message) == false &&
                   ActivatedServices.Any(s => s.IsActivated);
        }

        /// <summary>
        ///   Executes the share command.
        /// </summary>
        private void ExecuteShareCommand()
        {
            // TODO validation feedback
            if (CanExecuteShareCommand() == false)
                return;

            SaveSelectedService();

            // For each selected service, start the job
            foreach (var serviceVm in ActivatedServices.Where(s => s.IsActivated))
            {
                var account = _optionsManager.ActivatedServices.FirstOrDefault(s => s.ServicePackage == serviceVm.ServicePackage);
                if (account != null)
                    _serviceManager.AddShareJob(account, Message, Item);
            }

            Message = String.Empty;
            ExecuteOkayCommand();
        }
    }
}